<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    let array = [21,2,3,4,56,6,7,8,1];
    // let len = array.length;
    let n = 3;

    // let lineNum = len % n == 0 ? len / n : Math.floor((len / n) + 1)
    // let res = [];
    // for(let i = 0; i < lineNum; i++) {
    //   let temp = array.slice(i*n, i*n+n);
    //   res.push(temp)
    // }
    let res = convertOne_dimensionalArrayToTwo_dimensional(array,n);
    console.log(res);
    
    function convertOne_dimensionalArrayToTwo_dimensional(array,dimensional) {
      if(!array || array.length == 0) {
        return [];
      }
      let len = array.length;
      let lineNum = len % n == 0 ? len / n : Math.floor((len / n) + 1)
      let res = [];
      for(let i = 0; i < lineNum; i++) {
        let temp = array.slice(i*n, i*n+n);
        res.push(temp)
      }
      return res;
    }
    console.log(res.flat(Infinity));//将至一维
  </script>
</body>
</html>